<%@ include file="../import_page.jsp"%>
<%@ page import="org.json.*"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.admin)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//ChecK paramter
	String unitId = request.getParameter(PAR_UNIT_ID);
	String errMsg = null;
	
	UnitsControl unitCon = new UnitsControl();
	if(!isEmptyString(unitId)) {
		//if unit id provide, get unit detail
		try {
			//get unit info from database
			Units selectUnit = unitCon.getUnit(unitId);
			session.setAttribute(SESSION_SELECT_UNIT, selectUnit);
		} catch (Exception err) {
			//prepare to print error message
			errMsg = err.toString();
		}
	}

	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		unitCon.closeDBConnection();
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);

	ArrayList<Users> listUser = null;
	try {
		listUser = unitCon.getUsersFromUnit(selectUnit.getId());
	} catch (Exception err) {
		errMsg = err.toString();
	}
	
	unitCon.closeDBConnection();
%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<style type="text/css">
</style>
<link rel="stylesheet" type="text/css" href="../img_style.css" />
</head>

<body>
	<%if(!isEmptyString(errMsg)) { %>
	<p><%=errMsg %></p>
	<%} %>
	<p>
		Unit:
		<%=selectUnit.getName() %>
		(<%=selectUnit.getId() %>)
	</p>
	<hr />
	<table border="1">
		<tr>
			<th>Staff</th>
			<th>Student</th>
		</tr>
		<tr>
			<td>
				<ul>
					<%
Iterator<Users> luI =  listUser.iterator();
int numStaff = 0;
while(luI.hasNext()) {
	Users aUser = luI.next();
	if(aUser.getUserType() == UsersType.staff)
	{
		numStaff++;
%>
					<li><%=aUser.getName() %> (<%=aUser.getId() %>)</li>
					<%
	}
}
%>
				</ul></td>
			<td>
				<ul>
					<%
luI =  listUser.iterator();
int numStudent = 0;
while(luI.hasNext()) {
	Users aUser = luI.next();
	if(aUser.getUserType() == UsersType.student)
	{
		numStudent++;
%>
					<li><%=aUser.getName() %> (<%=aUser.getId() %>)</li>
					<%
	}
}
%>
				</ul></td>
		</tr>
		<tr>
			<td>Total Staff: <%=numStaff %></td>
			<td>Total Student: <%=numStudent %></td>
		</tr>
		<tr>
			<td colspan="2">Total: <%=listUser.size() %></td>
		</tr>
	</table>
</body>
</html>